import os

PROGRAM = "pylupdate4.exe"
SOURCES = "PsyBlockGame"
LOCALIZATION = r"PsyBlockGame\UI\localization.ts"

class DirWalker:
    def __init__(self, root, *ext):
        self.ext = ext
        self.root = root
        self.stack = [("", self.getDirs(self.root), 0)]

    @staticmethod
    def getDirs(path):
        dirs = os.listdir(path)
        dirs = zip(map(lambda x: os.path.isdir(path + os.sep + x), dirs), dirs)
        dirs = list(dirs)
        dirs.sort()
        return [p for _, p in dirs]

    def __iter__(self):
        while len(self.stack) > 0:
            path, dirs, index = self.stack[-1]

            if index >= len(dirs):
                self.stack.pop()
                continue

            name = dirs[index]
            self.stack[-1] = (path, dirs, index + 1)
            
            fullpath = r'%s\%s%s' % (self.root, path, name)

            if os.path.isdir(fullpath):
                if name[0] != '.':
                    self.stack.append(('%s%s\\' % (path, name), self.getDirs(fullpath), 0))
                continue

            if os.path.isfile(fullpath):
                _, e = os.path.splitext(name)
                if len(self.ext) == 0 or e in self.ext:
                    yield path, name, e

def main():
    files = [
        r'%s\%s\%s' % (SOURCES, path, file)
        for path, file, _ in DirWalker(SOURCES, '.py')
    ]

    cmd = r"%s %s -ts %s" % (PROGRAM, ' '.join(files), LOCALIZATION)
    print("Check first")
    print(cmd)
    #os.system(cmd)

if __name__ == '__main__':
    main()
  